package com.base.cn.platform.os.service.op.advert;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.op.advert.OpAdvertDao;
import com.base.cn.platform.os.entity.op.advert.OpAdvert;
import com.base.cn.platform.os.entity.op.advert.OpAdvertPlace;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.manage.user.SysUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 广告Biz
 *
 * @author s.li
 * @create 2017-07-31-18:49
 */
@Service
public class OpAdvertBiz extends BaseBiz<OpAdvert,OpAdvertDao> {
    @Autowired
    private OpAdvertPlaceBiz opAdvertPlaceBiz;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SysUserBiz sysUserBiz;

    /**
     * 保存修改广告
     * @param advert
     * @return
     */
    public OpAdvert saveUpdateAdvert(OpAdvert advert){
        if(DataUtil.idIsNotNull(advert.getId())){
            advert.setStatus(null);
            this.updateByWhereSql(advert," id = " + advert.getId());
        }else {
            advert.setStatus(1);
            this.save(advert);
        }
        return advert;
    }

    /**
     * 多条件查询广告数据列表
     * @param advert 查询条件
     * @return 广告数据列表
     */
    public PageInfo<OpAdvert> queryAdvertListPage(OpAdvert advert, Pagination page,boolean courseDate,boolean createUserData,boolean advertPlaceData){
        String whereSql =whereSql(advert);
        PageInfo<OpAdvert> pageInfo =this.findPage(whereSql,page,null);
        pageInfo.setList(setOpAdvertPlaceAndCourseAndCreateUserList(pageInfo.getList(),courseDate,createUserData,advertPlaceData));
        return pageInfo;
    }


    /**
     * Sql语句
     * @param advert
     * @return
     */
    public String whereSql (OpAdvert advert){
        StringBuilder whereSql = new StringBuilder(" 1=1");
        if(ObjectUtils.isNotEmpty(advert)){
            if(ObjectUtils.isNotEmpty(advert)){
                if(StringUtils.isNotEmpty(advert.getPlaceKey())){
                    whereSql.append(" and placeKey = '").append(advert.getPlaceKey().trim()).append("'");
                }
                if(advert.getStatus()!=null&&advert.getStatus()>0){
                    whereSql.append(" and status = ").append(advert.getStatus());
                }
                if(advert.getRedirectType()!=null&&advert.getRedirectType()>0){
                    whereSql.append(" and redirectType = ").append(advert.getRedirectType());
                }
                if(StringUtils.isNotEmpty(advert.getDepict())){
                    whereSql.append(" and depict like '%").append(advert.getDepict().trim()).append("%'");
                }
            }
            whereSql.append(" order by sort desc");
        }
        return whereSql.toString();
    }

    /**
     * 获取广告列表
     * @param advert 查询条件
     * @return 广告数据列表
     */
    public List<OpAdvert> queryAdvertList(OpAdvert advert,boolean courseDate,boolean createUserData,boolean advertPlaceData){
        String whereSql = whereSql(advert);
        List<OpAdvert> opAdverts =this.find(whereSql,null,null);
        opAdverts = setOpAdvertPlaceAndCourseAndCreateUserList(opAdverts,courseDate,createUserData,advertPlaceData);
        return opAdverts;
    }


    /**
     * 获取广告列表
     * @param id 查询条件
     * @return 广告数据列表
     */
    public OpAdvert queryAdvertById(BigDecimal id,boolean courseDate,boolean createUserData,boolean advertPlaceData){
        OpAdvert opAdvert =this.findById(id);
        opAdvert = setOpAdvertPlaceAndCourseAndCreateUser(opAdvert,courseDate,createUserData,advertPlaceData);
        return opAdvert;
    }

    /**
     * 单个set扩展数据
     * @param advert
     * @param courseDate
     * @param createUserData
     * @param advertPlaceData
     * @return
     */
    public OpAdvert setOpAdvertPlaceAndCourseAndCreateUser(OpAdvert advert,boolean courseDate,boolean createUserData,boolean advertPlaceData){
        if (ObjectUtils.isNotEmpty(advert)){
            List<OpAdvert> opAdverts = new ArrayList<>();
            opAdverts.add(advert);
            opAdverts = setOpAdvertPlaceAndCourseAndCreateUserList(opAdverts,courseDate,createUserData,courseDate);
            return opAdverts.get(0);
        }
        return advert;
    }



    /**
     * set广告位置信息，课程，创建用户
     * @param advertList 广告列表
     * @param courseDate 课程  false  true
     * @param createUserData  创建用户 false  true
     * @param advertPlaceData 广告位置信息 false  true
     * @return
     */
    public List<OpAdvert> setOpAdvertPlaceAndCourseAndCreateUserList(List<OpAdvert> advertList,boolean courseDate,boolean createUserData,boolean advertPlaceData){
        if (ObjectUtils.isNotEmpty(advertList)){
            if (courseDate){
                advertList = setAdvertCourseList(advertList);
            }
            if (createUserData){
                sysUserBiz.setSysUserData(advertList,createUserData);
            }
            if (advertPlaceData){
                advertList = setAdvertPlaceList(advertList);
            }
        }
        return advertList;
    }

    /**
     * 设置广告的位置信息
     * @param advertList 广告数据
     */
    public List<OpAdvert> setAdvertPlaceList(List<OpAdvert> advertList){
        if(ObjectUtils.isNotEmpty(advertList)){
            Map<String,OpAdvertPlace> advertPlaceMap = null;
            String keys = advertList.stream().map(e->e.getPlaceKey()).collect(Collectors.joining("','","'","'"));
            if (StringUtils.isNotEmpty(keys)){
                List<OpAdvertPlace> advertPlaceList = opAdvertPlaceBiz.find(" placeKey in ("+keys+")",null,null);
                advertPlaceMap = advertPlaceList.stream().collect(Collectors.toMap(OpAdvertPlace::getPlaceKey,e->e));
                if(ObjectUtils.isNotEmpty(advertPlaceMap)){
                    for(OpAdvert advert : advertList){
                        OpAdvertPlace advertPlace = advertPlaceMap.get(advert.getPlaceKey());
                        advert.setAdvertPlace(advertPlace);
                    }
                }
            }
        }
        return advertList;
    }

    /**
     * 设置广告创建人数据
     * @param advertList 广告数据
     */
    public List<OpAdvert> setAdvertCourseList(List<OpAdvert> advertList){
        if(ObjectUtils.isNotEmpty(advertList)){
            String courseIds = advertList.stream().map(e->e.getCourseId().toString()).collect(Collectors.joining(","));
            if (StringUtils.isNotEmpty(courseIds)){
                Map<BigDecimal,Map<String,Object>> coursesMap = courseService.findCourseToMapByIds(courseIds,false,false,false);
                if(ObjectUtils.isNotEmpty(coursesMap)){
                    for(OpAdvert advert : advertList){
                        Map<String,Object> courseMap = coursesMap.get(advert.getCourseId());
                        advert.setCourseMap(courseMap);
                    }
                }
            }
        }
        return advertList;
    }

    /**
     * 删除广告
     * @param ids 广告ID串
     */
    public void deleteAdvert(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            ids = StringUtils.subHeadTailString(ids,",");
            if(StringUtils.isNotEmpty(ids)) {
                //删除广告
                this.deleteWhereSql(" id in (" + ids + ")");
            }
        }
    }

    /**
     * 修改广告状态
     * @param ids OpAdvertIDS
     * @param status 状态
     */
    public void updateOpAdvertStatus(String ids,Integer status){
        ids = StringUtils.subHeadTailString(ids,",");
        if(StringUtils.isNotEmpty(ids)){
            OpAdvert advert = new OpAdvert();
            advert.setStatus(status);
            this.updateByWhereSql(advert," id in ("+ids+")");
        }
    }
}
